package client;

import gui.Cart_Gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class ItemsInCustomItem extends JFrame implements ActionListener 
{
	public setImage img = null;
	public JButton Back = null;
	public static JTable table = null;
	public JScrollPane ScrollPane = null;
	public Cart userCart = null;
	public JLabel CustomItemName = null;
	public String[] columnNames = {"Id", "Name", "Price Per Item", "Type", "Quantity", "Image", "Remove"};
	public Object[][] Data = null;
	public JButton removeItem = new JButton();
	public RemoveItems remove = null;
	int width = (int)Main.dim.getWidth();
	int height = (int)Main.dim.getHeight();
	
	public ItemsInCustomItem(Object[][] data)
	{
		img = new setImage("customItem");
		img.setLayout(null);
		
		this.setTitle("Custom Item");
		this.setPreferredSize(new Dimension(width-(width/2)+175, height-(height/3)-100));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setResizable(false);
		
		final DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();  
        dtcr.setHorizontalTextPosition(JLabel.CENTER);  
        
        String[] split = new String[2];
        split = ((String)Cart_Gui.customItemsTable.getValueAt(Cart_Gui.customItemsTable.getSelectedRow(), 1)).split("-");
        
        CustomItemName = new JLabel(split[1]+" ("+split[0]+")");
        CustomItemName.setFont(new Font (Font.SANS_SERIF ,Font.BOLD,25));
        CustomItemName.setBounds(10,80, 300, 40);
		img.add(CustomItemName);
		
        Data = data;
        
		table = new JTable(new MyTableModel(Data, columnNames, "ItemsInCustomItem"));
	    table.setPreferredScrollableViewportSize(new Dimension(450, 200));
        table.setFillsViewportHeight(true);
        table.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
        table.setIntercellSpacing(new Dimension(30,30));
		table.getTableHeader().setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,20));
		table.getTableHeader().setForeground(new Color(92,64,51));
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        table.getColumnModel().getColumn(0).setCellRenderer(dtcr);
        table.getColumnModel().getColumn(2).setCellRenderer(dtcr);
        table.getColumnModel().getColumn(4).setCellRenderer(dtcr);
        table.setBackground(new Color(253,245,230));
        
        table.setRowHeight(100);
        
        table.getColumnModel().getColumn(6).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(6).setCellEditor(new ButtonEditor(new JCheckBox()));
        removeItem.addActionListener(this);
        
        ScrollPane = new JScrollPane(table);
        ScrollPane.setBounds(10, 120, width-(width/2)+150, height-(height/3)-270);
		img.add(ScrollPane);
		
		Back = new JButton("Back");
		Back.setBounds((int)this.getPreferredSize().getWidth()-115,10,100,40);
		Back.addActionListener(this);
		img.add(Back);
		
		this.addWindowListener(new WindowAdapter() 
        {public void windowClosing(WindowEvent e)
        {Back.doClick();}});
		
		this.add(img);
		this.pack();
		
		this.setEnabled(true);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == removeItem)
		{
			if(remove != null)
				remove.dispose();
			remove = new RemoveItems("ItemsInCustomItem");
		}
		
		if(e.getSource() == Back)
		{
			if(Cart_Gui.callFrom.equals("MainScreen"))
				Main.main.cart.doClick();

			else if(Cart_Gui.callFrom.equals("Catalog"))
					Main.catalog.goToCart.doClick();		
			
			else if(Cart_Gui.callFrom.equals("CustomOrder"))
					Main.customOrder.goToCart.doClick();
			this.dispose();
		}
	}
	
	class ButtonRenderer extends JButton implements TableCellRenderer 
    {
    	public ButtonRenderer() 
    	{setOpaque(true);}
    	public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
    	{	
    		setText("Remove");
    		return this;
    	}
    }
    
    class ButtonEditor extends DefaultCellEditor 
    {
    	private String label;
    	public ButtonEditor(JCheckBox checkBox) 
    	{
    		super(checkBox);
    	}
    	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 
    	{
    		label = "Remove";
    		removeItem.setText(label);
    		return removeItem;
    	}
    	public Object getCellEditorValue() 
    	{
    		return new String(label);
    	}
    }
}
